
private ["_integerPart", "_string", "_numIntegerDigits", "_decimalPoint",
	"_thousandsSeparator"];

_number=_this select 0;

_integerWidth=1;
_decimalPlaces=0;
_separateThousands=false;

if (count _this>0) then {_integerWidth=_this select 1;};
if (count _this>1) then {_decimalPlaces=_this select 2};
if (count _this>2) then {_separateThousands=_this select 3};


_decimalPoint = ",";
_thousandsSeparator = ".";
	
// Start by working out how to display the integer part of the number.
if (_decimalPlaces > 0) then
{
	_integerPart = floor (abs _number);
}
else
{
	_integerPart = round (abs _number);
};

_string = "";
_numIntegerDigits = 0;

while {_integerPart > 0} do
{
	if ((_numIntegerDigits > 0) and ((_numIntegerDigits mod 3) == 0) and _separateThousands) then
	{
		_string = _thousandsSeparator + _string;
	};
	
	_string =  (str (_integerPart mod 10)) + _string;
	_numIntegerDigits = _numIntegerDigits + 1;
	
	_integerPart = floor (_integerPart / 10);
};

// Pad integer with 0s
while {_numIntegerDigits < _integerWidth} do
{
	if ((_numIntegerDigits > 0) and ((_numIntegerDigits mod 3) == 0) and _separateThousands) then
	{
		_string = _thousandsSeparator + _string;
	};
	
	_string = "0" + _string;
	
	_numIntegerDigits = _numIntegerDigits + 1;
};

// Add a - sign if needed.
if (_number < 0) then
{
	_string = "-" + _string;
};

// Add decimal digits, if necessary.
if (_decimalPlaces > 0) then
{
	private ["_digit", "_multiplier", "_i"];
	
	_string = _string + _decimalPoint;
	
	_multiplier = 10;
	
	for "_i" from 1 to _decimalPlaces do
	{
		if (_i == _decimalPlaces) then
		{
			_digit = round ((_number * _multiplier) mod 10);
		}
		else
		{
			_digit = floor ((_number * _multiplier) mod 10);
		};

		// If the digit has become infintesimal, pad to the end with zeroes.
		if (not (finite _digit)) exitWith
		{
			private "_j";
			
			for "_j" from _i to _decimalPlaces do
			{
				_string = _string + "0";
			};
		};
		
		_string = _string + (str _digit);
		
		_multiplier = _multiplier * 10;
	};
};

_string; // Return.
